﻿package org.catapult.eslide {
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import mx.containers.HBox;
	import mx.controls.Alert;
	import mx.controls.LinkButton;
	import mx.core.Application;
	import mx.core.Repeater;
	import mx.events.FlexEvent;
	import org.catapult.eslide.module.ExGalleryCanvas;
	import org.catapult.eslide.module.GalleryCanvas;
	import org.catapult.event.XMLServiceEvent;
	import org.catapult.service.XMLService;
	
	/**
	 * ...
	 * @author Lam Do
	 */
	public class MainApplication extends Application{
		[Bindable]
		private var _xml: XML;
		
		[Bindable]
		public var gallery: ExGalleryCanvas;
		[Bindable]
		public var labelArray: Array;
		public var buttonsHBox: HBox;
	
	
		public function MainApplication() {
			super();
			
			this.addEventListener(FlexEvent.APPLICATION_COMPLETE, applicationCompleteHandler);
		}
		
		private function applicationCompleteHandler(e:FlexEvent):void {
			this.removeEventListener(FlexEvent.APPLICATION_COMPLETE, applicationCompleteHandler);
			this.loadGallery("gallery.xml");
		}
		
		private function loadGallery(url: String): void {
			var service:XMLService = new XMLService();
			service.addEventListener(XMLServiceEvent.XML_COMPLETE, xmlCompleteHandler);
			service.execute(url);
			
		}
		
		private function xmlCompleteHandler(e:XMLServiceEvent):void {
			xml = e.xml;
			var itemList: XMLList = xml.item as XMLList;
			
		/*	var i:uint = 1;
			for each (var xmlItem: XML in itemList) {
				var link: LinkButton = new LinkButton();
				link.label = i.toString();
				link.id = (i - 1).toString();
				link.addEventListener(MouseEvent.CLICK, openLinkHandler);
				link.addEventListener(MouseEvent.ROLL_OVER, rollOverLinkHandler);
				link.addEventListener(MouseEvent.ROLL_OUT, rollOutLinkHandler);
				buttonsHBox.addChild(link);								
				i++;
			} */
		}
		
		/*private function rollOutLinkHandler(e:MouseEvent):void {
			gallery.hideThumbnail();
		}
		
		private function rollOverLinkHandler(e:MouseEvent):void {
			
			var rollOverPoint: Point = new Point (e.localX, e.localY);
			var globalPoint: Point = e.currentTarget.localToGlobal(rollOverPoint);
			
			gallery.showThumbnail(e.currentTarget.id, globalPoint.x, globalPoint.y);
		}*/
		
		private function openLinkHandler(e:MouseEvent):void {
			gallery.slideIndex = e.currentTarget.id;
		}
		
		[Bindable]
		public function get xml():XML { return _xml; }
		
		public function set xml(value:XML):void {
			_xml = value;
		}
		
	}

}